<?php

class Users_IndexController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
    	$modelUsers = new Users();
    	$users = $modelUsers->getUsers();
        $this->view->users = $users;
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $userId = $this->_getParam('userId');

        $modelUsers = new Users();
        $user = $modelUsers->getUser($userId);

        $this->view->user = $user;
    }
    
    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelUsers = new Users();
    	$users = $modelUsers->getUsers();
        $this->view->users = $users;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formUser = new FormUsers('');
    		if ($formUser->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$user = new Users();
    			$user->setDataFromArray($post);
    			if ($user->id) {
	    			$user->update();
	    		} else {
    				$user->insert();
	    		}
	    		$this->_helper->redirector->gotoRoute(array(), 'adminUsers');
    		}
			$this->view->formUsers = $formUser;
    	} else {
    		$id = $this->_getParam('userId');
        	$modelUsers = new Users();
        	$user = $modelUsers->getUser($id);
			$formUsers = new FormUsers($user);
			$this->view->formUsers = $formUsers;
    	}
    }

    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('userId');
        $modelUsers = new Users();
        $res = $modelUsers->delete('id='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminUsers');
    }
}

